﻿@using DBMS.Models;

@{
    ViewBag.Title = "BrowseTable";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<script type="text/javascript">
    $(document).ready(function () {
        $('#saveButton').hide();

        var first = true;

        $('#addButton').click(function () {
            $('#saveButton').show();

            $('#tableBody').append('<tr>');

            @foreach (Column column in ViewBag.Columns) 
            {
                <text>
                    $('#tableBody').append('<td><input type="text" name="values" class="textbox" value="" /></td>');
                </text>
            }

            $('#tableBody').append('</tr>');

            first = false;
        });

        $('#saveButton').click( function() {
            $("input[type=text][value]").each(function() {
                var input = $(this).val();
                $("form").append($(input));
            });
        });
    });
</script>

<h2>BrowseTable</h2>

@if (ViewBag.ErrorMessage != null)
{
    <div>@ViewBag.ErrorMessage</div>
}

@{
    string actionString = "/Values/CreateValues?databaseId="+ ViewBag.Database.Index + "&tableId=" + ViewBag.Table.Index;
}

<form action="@(actionString)" method="post">
<table>
    <thead>
        <tr>
            @foreach (Column column in ViewBag.Columns)
            {
                <td>@column.Name</td>
            }
        </tr>
    </thead>
    <tbody id="tableBody">
        @if (ViewBag.RowIds.Count > 0)
        {

            foreach (int rowId in ViewBag.RowIds)
            {
                <tr>
                    @{
                        int counter = 0;
                        
                        foreach (Value value in ViewBag.Values)
                        {
                            if (value.RowId == rowId)
                            {
                                counter++;
                                <td>@value.ConcreteValue</td>
                            }
                        }
                        
                        int columns = ViewBag.Columns.Count;
                        
                        if (counter < columns)
                        {
                            for (int i = 0; i < (columns - counter); i++)
                            {
                                <td> </td>
                            }
                        }
                    }



                    <td>
                        @Html.ActionLink("Edit", "EditRow", "Values", new { databaseId = ViewBag.Database.Index.ToString(), tableId = ViewBag.Table.Index, rowId = rowId }, null)
                        @Html.ActionLink("Drop", "DeleteRow", "Values", new { databaseId = ViewBag.Database.Index.ToString(), tableId = ViewBag.Table.Index, rowId = rowId }, null)
                    </td>
                </tr>

            }
        }
    </tbody>
</table>
<input type='submit' value='Save' id='saveButton' />
</form>

@if (ViewBag.Columns.Count > 0)
{
    <input type='button' value='Add New Row' id='addButton' />
}
else
{
    <div>There are no columns in this table.</div>
}
